#!/bin/sh

# wait to start web and run from goahead code
. /etc/scripts/web_wait.sh
web_wait

# get params
. /etc/scripts/global.sh

LOG="logger -t kext"

start() {
    nat_mode
    hw_nat_en
    pthrough
    # This is set only if wan or lan in bridge
    if [ "$wan_if" = "br0" ] || [ "$lan_if" = "br0" ]; then
	bridge_stp
	bridge_fastpath
    fi
    # Only if VPN enabled
    if [ "$vpnEnabled" = "on" ]; then
	vpn_limit
    fi
}

nat_mode() {
    eval `nvram_buf_get 2860 nat_mode`
    if [ "$nat_mode" = "0" ]; then
	sysctl -w net.nf_conntrack_nat_mode=0
	$LOG "Nat mode Linux"
    elif [ "$nat_mode" = "2" ]; then
	sysctl -w net.nf_conntrack_nat_mode=2
	$LOG "Nat mode Restricted Cone"
    else
	sysctl -w net.nf_conntrack_nat_mode=1
	$LOG "Nat mode Full Cone"
    fi
}

hw_nat_en() {
    # In bridge and chillispot mode not fastnat use
    if [ "$OperationMode" != "0" ] && [ "$OperationMode" != "4" ] && [ "$ApCliBridgeOnly" != "1" ]; then
	eval `nvram_buf_get 2860 hw_nat_bind hw_nat_wifi natFastpath`
	# select NAT offload mode
	if [ "$natFastpath" = "1" ]; then
	    hw_nat=0
	    sw_nat=1
	    $LOG "NAT Offload mode SW_FASTPATH"
	elif [ "$natFastpath" = "2" ]; then
	    hw_nat=1
	    sw_nat=0
	    $LOG "NAT Offload mode HW_NAT"
	elif [ "$natFastpath" = "3" ]; then
	    hw_nat=1
	    sw_nat=1
	    $LOG "NAT Offload mode COMPLEX"
	else
	    hw_nat=0
	    sw_nat=0
	    $LOG "NAT Offload mode DISABLE"
	fi
	if [ "$QoSEnable" != "" -a "$QoSEnable" != "0" ]; then
	    hw_nat=0
	    $LOG "QoS enabled - HW_NAT Offload mode DISABLE"
	fi
	if [ "$hw_nat_wifi" = "1" ]; then
	    hw_nat_wifi="wifi_offload=1"
	    $LOG "WiFi HW_NAT Offload mode ENABLE"
	else
	    hw_nat_wifi="wifi_offload=0"
	    $LOG "WiFi HW_NAT Offload mode DISABLE"
	fi
	# configure nat offload
	if [ "$hw_nat" = "1" ]; then
	    if [ ! -d /sys/module/hw_nat ]; then
		modprobe -q hw_nat "$hw_nat_wifi"
	    fi
	    # set HW_NAT default QOS Mode to WRR policy
	    hw_nat -L 0
	else
	    if [ -d /sys/module/hw_nat ]; then
		rmmod hw_nat > /dev/null 2>&1
	    fi
	fi
	# configure software nat offload
	if [ "$sw_nat" = "1" ]; then
	    sysctl -w net.nf_conntrack_fastnat=1
	else
	    sysctl -w net.nf_conntrack_fastnat=0
	fi
	# if any QoS/Shaper mode and hw_nat ebables need increase binding threshold
	if [ "$QoSEnable" != "" ] && [ "$QoSEnable" != "0" ]; then
	    hw_nat_bind=250
	fi
	# set hw_nat binding threshold if user set not default value
	if [ -d /sys/module/hw_nat ] && [ "$hw_nat" = "1" ] && [ "$hw_nat_bind" != "" ] && [ "$hw_nat_bind" != "30" ]; then
	    $LOG "Increase HW_NAT binding threshold to $hw_nat_bind."
	    hw_nat -N $hw_nat_bind > /dev/null 2>&1
	fi
    else
	$LOG "NAT Offload mode DISABLE"
	sysctl -w net.nf_conntrack_fastnat=0
	if [ -d /sys/module/hw_nat ]; then
	    rmmod hw_nat > /dev/null 2>&1
	fi
    fi
}

pthrough() {
    # In bridge and chillispot mode not passth use
    if [ "$OperationMode" != "0" ] && [ "$OperationMode" != "4" ] && [ "$ApCliBridgeOnly" != "1" ]; then
	eval `nvram_buf_get 2860 pppoe_pass ipv6_pass`
	# pppoe and ip_v6 kernel mode relay.
	if [ -d /proc/pthrough ]; then
	    if [ "$pppoe_pass" = "1" ]; then
		$LOG "PPPOE Pass Through enable for $lan_if and $wan_if interfaces."
		echo "$lan_if,$wan_if" > /proc/pthrough/pppoe
	    else
		echo "null,null" > /proc/pthrough/pppoe
	    fi
	    if [ "$ipv6_pass" = "1" ]; then
		$LOG "IPv6 Pass Through enable for $lan_if and $wan_if interfaces."
		echo "$lan_if,$wan_if" > /proc/pthrough/ipv6
	    else
		echo "null,null" > /proc/pthrough/ipv6
	    fi
	fi
    else
	if [ -d /proc/pthrough ]; then
	    echo "null,null" > /proc/pthrough/pppoe
	    echo "null,null" > /proc/pthrough/ipv6
	fi
    fi
}

vpn_limit() {
    vpnCpuLimit=`nvram_get 2860 vpnCpuLimit`
    if [ "$vpnCpuLimit" != "" ] && [ "$vpnCpuLimit" != "0" ]; then
	$LOG "Limit cpu load for PPP to $vpnCpuLimit ticks"
	sysctl -w net.ipv4.ppp_cpu_load_limit=$vpnCpuLimit
    else
	sysctl -w net.ipv4.ppp_cpu_load_limit=0
    fi
}

bridge_fastpath() {
    #----Fastpath for bridge enable------------------------------
    bridgeFastpath=`nvram_get 2860 bridgeFastpath`
    if [ "$bridgeFastpath" != "0" ]; then
	$LOG "Bridge fastpath enabled."
	sysctl -w net.ipv4.bridge_fastpath=1
    else
	sysctl -w net.ipv4.bridge_fastpath=0
    fi
}

bridge_stp() {
    #----Bridge STP----------------------------------------------
    stpEnabled=`nvram_get 2860 stpEnabled`
    if [ "$stpEnabled" = "1" ]; then
	$LOG "Bridge STP enabled."
	brctl setfd br0 15
	brctl stp br0 1
    else
	brctl setfd br0 1
	brctl stp br0 0
    fi
}

stop() {
    :
}

case "$1" in
	start)
	    start
	    ;;

	stop)
	    stop
	    ;;

	restart)
	    stop
	    start
	    ;;

	*)
	    echo $"Usage: $0 {start|stop|restart}"
	    exit 1
esac
